/**
 * 
 */
package com.iwords.service.persistence.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.iwords.service.object.ExamSentencesEntity;
import com.iwords.service.persistence.ExamSentencesPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-5-1    
 * @description         
 */
@Stateless
public class ExamSentencesPersistenceImpl implements ExamSentencesPersistence {

	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	public ExamSentencesEntity getSentenceById(Long id) {
		Query query = getEntityManager().createQuery("SELECT u from ExamSentencesEntity u where u.sentenceid=?1  ");
		query.setParameter(1, id);

		return (ExamSentencesEntity) query.getSingleResult();
	}

	public void updateSentence(ExamSentencesEntity sentence) {
		getEntityManager().createQuery("update ExamSentencesEntity l set l.paraphrase=?1   where l.sentenceid=?2").setParameter(1, sentence.getParaphrase())
				.setParameter(2, sentence.getSentenceid()).executeUpdate();

	}

	@SuppressWarnings("unchecked")
	public List<ExamSentencesEntity> getSentenceList(String ids) {
		Query query = getEntityManager().createQuery("SELECT u from ExamSentencesEntity u where u.sentenceid in( " + ids + ")");

		return query.getResultList();

	}

	 
	public void updateSentences(List<ExamSentencesEntity> sentences) {

		for (ExamSentencesEntity entity : sentences) {
			updateSentence(entity);
		}

	}
}
